unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons;

const
N = 20;
W = 20;
H = 10;

type
  TForm1 = class(TForm)
    Image1: TImage;
    BitBtn1: TBitBtn;
    Timer1: TTimer;
    ScrollBar1: TScrollBar;
    procedure Timer1Timer(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mass: array of integer;
  i, j: integer;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  setlength (mass, N);
  randomize;
  for i:= 0 to n-1 do
    mass[i]:=random (50)+1;
  i:= 0;
  j:= 0;
  Timer1.interval:=Scrollbar1.Position;
  Timer1.Enabled := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var t, g, k, l: integer;
begin
  k:=0;
  Timer1.Interval:=Scrollbar1.Position;
  if (i = n-1-j) then
  begin
    i:= 0;
    j:=j+1;
  end;
  //for i:= 0 to N-2 do
  //begin
      if (mass[i] > mass[i+1]) then
      begin
        t:= mass[i];
        mass[i]:= mass[i+1];
        mass[i+1]:= t;
      end;
      Image1.Canvas.Brush.Color:= clWhite;
      Image1.Canvas.Pen.Color:= clWhite;
      Image1.Canvas.Rectangle(0, 0, Image1.Width, Image1.Height);
      Image1.Canvas.Pen.Color:= clBlack;
      for g:= 0 to N-1 do
      begin
        Image1.Canvas.Rectangle(0, g*W, H*mass[g], (g+1)*W);
      end;
      Image1.Canvas.Brush.Color:= clRed;
      Image1.Canvas.Rectangle(0, i*W, H*mass[i], (i+1)*W);
      Image1.Canvas.Rectangle(0, (i+1)*W, H*mass[i+1], (i+1+1)*W);
  //end;
  i:= i+1;
  for l:= 0 to N-2 do
  begin
    if (mass[l] > mass[l+1]) then
      begin
        k:=1;
      end;
  end;
  if (k=0) then
  begin
    Image1.Canvas.Brush.Color:= clWhite;
    for g:= 0 to N-1 do
    begin
      Image1.Canvas.Rectangle(0, g*W, H*mass[g], (g+1)*W);
    end;
    Timer1.Enabled := false;
  end;
end;


end.
